﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeFrame.AutoCode
{
    class BLLHelper
    {

        public string GenerateBLLText(TableEntity tableEntity, List<ColumnEntity> columnList, ConfigParameter configParameter)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using WeFrame.Core.Common;");
            sb.AppendLine("using WeFrame.Core.DAL;");
            sb.AppendLine("using WeFrame.Core.Model;");
            sb.AppendLine("using " + configParameter.NameSpaceTag + ".Model;");
            sb.AppendLine("using " + configParameter.NameSpaceTag + ".DAL;");
            sb.AppendLine("");
            sb.AppendLine("namespace "+configParameter.NameSpaceTag+"."+configParameter.BllTag);
            sb.AppendLine("{");
            sb.AppendLine("    /// <summary>");
            sb.AppendLine("    /// 管理员信息表");
            sb.AppendLine("    /// </summary>");
            sb.AppendLine("    public partial class "+tableEntity.TableName+configParameter.BllTag);
            sb.AppendLine("    {");
            sb.AppendLine("");
            sb.AppendLine("        private readonly " + tableEntity.TableName + "DAL dal;");
            sb.AppendLine("        public " + tableEntity.TableName +configParameter.BllTag+ "()");
            sb.AppendLine("        {");
            sb.AppendLine("            dal = new " + tableEntity.TableName + "DAL();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        #region 基本方法=============================");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 是否存在该记录");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public bool Exists(int id)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.Exists(id);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 增加一条数据");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public int Add(" + tableEntity.TableName + "Entity model)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.Add(model);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 更新一条数据");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public bool Update(" + tableEntity.TableName + "Entity model)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.Update(model);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 删除一条数据");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public bool Delete(int id)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.Delete(id);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("        public void Delete(string ids)");
            sb.AppendLine("        {");
            sb.AppendLine("            string[] idArray= ids.Split(',');");
            sb.AppendLine("            foreach (string id in idArray) ");
            sb.AppendLine("            {");
            sb.AppendLine("                this.Delete(int.Parse(id));");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 得到一个对象实体");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        public " + tableEntity.TableName + "Entity GetModel(int id)");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.GetModel(id);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 查询对象集合");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"queryParamList\">查询条件集合</param>");
            sb.AppendLine("        /// <param name=\"queryOrderList\">排序条件集合</param>");
            sb.AppendLine("        /// <param name=\"queryPage\">分页</param>");
            sb.AppendLine("        /// <returns>数据集合</returns>");
            sb.AppendLine("        public DataSet QueryDataSet(List<QueryParam> queryParamList, List<QueryOrder> queryOrderList, QueryPage queryPage) ");
            sb.AppendLine("        {");
            sb.AppendLine("            return dal.QueryDataSet(queryParamList, queryOrderList, queryPage);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        #endregion");
            sb.AppendLine("    }");
            sb.AppendLine("}");
            return sb.ToString();

        }

    }
}
